load("~/Dropbox/professional/Research/Active/SciInRegs/Papers/Network/Analysis/SimNets.RData")
load("~/Dropbox/professional/Research/Active/SciInRegs/Papers/Network/Analysis/networkClean.RData")
library(sna)
library(network)

nodes <- nodeTitles

amat <- symmetrize(amat)

grange <- range(which(substr(nodes,1,2)=="g_")) # regulations
rrange <- range(which(substr(nodes,1,2)=="r_"))	# articles

verts <- c(rrange[1]:rrange[2],grange[1]:grange[2])
ArtNet <- amat[verts,verts]

artIsol <- (rrange[1]:rrange[2])[which(degree(ArtNet)[1:length(rrange[1]:rrange[2])] == 0)]

nodes <- nodes[-artIsol]
amat <- amat[-artIsol,-artIsol]

doti <- which(nodes =="a_departmentoftheinterior")
doi <- which(nodes == "a_departmentofinterior")
amat[doi,] <- amat[doi,]+amat[doti,]
amat[,doi] <- amat[,doi]+amat[,doti]

nodes <- nodes[-doti]
amat <- amat[-doti,-doti]

#write.csv(cbind(nodeTitles,""),"~/Dropbox/professional/Research/Active/SciInRegs/Papers/Network/Analysis/nodeList.csv",row.names=F)

library(colorRamps)
require(sna)

grange <- range(which(substr(nodes,1,2)=="g_")) # regulations
jrange <- range(which(substr(nodes,1,2)=="j_"))	# journals
rrange <- range(which(substr(nodes,1,2)=="r_"))	# articles
srange <- range(which(substr(nodes,1,2)=="s_")) # scientists
frange <- range(which(substr(nodes,1,2)=="f_")) # funders
arange <- range(which(substr(nodes,1,2)=="a_")) # agencies
crange <- range(which(substr(nodes,1,2)=="c_")) # committees
urange <- range(which(substr(nodes,1,2)=="u_")) # affiliations

# Node names
gnames <- nodes[grange[1]:grange[2]] # regulations
jnames <- nodes[jrange[1]:jrange[2]]	# journals
rnames <- nodes[rrange[1]:rrange[2]]	# articles
snames <- nodes[srange[1]:srange[2]] # scientists
fnames <- nodes[frange[1]:frange[2]] # funders
anames <- nodes[arange[1]:arange[2]] # agencies
cnames <- nodes[crange[1]:crange[2]] # committees
unames <- nodes[urange[1]:urange[2]] # affiliations

library(network)

artRIABipartite <- amat[(rrange[1]:rrange[2]),(grange[1]:grange[2])]
artRIANet <- network(artRIABipartite)

net <- artRIABipartite

simBipartite <- function(net){
	# net is an n1 x n2 matrix
	deg1 <- apply(net,1,sum)
	deg2 <- apply(net,2,sum)
	rows <- NULL
	cols <- NULL
	for(i in 1:length(deg1)){
		rows <- c(rows,rep(i,deg1[i]))
	}
	for(i in 1:length(deg2)){
		cols <- c(cols,rep(i,deg2[i]))
	}
	rrows <- sample(rows,length(rows))
	rcols <- sample(cols,length(cols))
	rnet <- matrix(0,length(deg1),length(deg2))
	edges <- cbind(rrows,rcols)
	# Randomly re-wire the duplicates
	newEdges <- edges
	dupEdges <- which(duplicated(edges))
	while(length(dupEdges)>0){
		ei <- newEdges[dupEdges[1],]
		othEdges <- newEdges[which(newEdges[,1]!=ei[1] & newEdges[,2] != ei[2]),]
		unDup <- F
		rothEdges <- othEdges[sample(1:nrow(othEdges),nrow(othEdges)),]
		othInd <- 1
		while(!unDup){
			eswap <- rothEdges[othInd,]
			unique <- !any(newEdges[,1]==eswap[1] & newEdges[,2]==ei[2]) & !any(newEdges[,1]==ei[1] & newEdges[,2]==eswap[2]) 
			if(unique){
				swapind <- which(newEdges[,1]==eswap[1] & newEdges[,2]==eswap[2])
				newEdges[swapind,2] <- ei[2]
				newEdges[dupEdges[1],2] <- eswap[2]
				unDup <- T
			}
			othInd <- othInd + 1
		}
		dupEdges <- which(duplicated(newEdges))
		print(length(dupEdges))
	}
	rnet[newEdges] <- 1
	rnet
}

set.seed(12345)

simNets <- list()
for(i in 1:1000){
	sim1 <- simBipartite(artRIABipartite)
	simNets[[i]] <- sim1
}

library(sna)
detach(package:sna)
source('~/Dropbox/professional/Research/Active/SciInRegs/Papers/Network/Analysis/NetSims/totalPathReduction.R')
type <- c(rep("art",rrange[2]-rrange[1]+1),rep("reg",grange[2]-grange[1]+1))
simBCArt <- list()
## Stopped it at 500 on 11/14/2014
for(i in 500:length(simNets)){
	net <- network(simNets[[i]])[,]
	st <- system.time(bci <- pathMembership(net,which(type=="art"),which(type=="reg")))
	simBCArt[[i]] <- bci
	print(i)
	print(st)
}

save(list="simNets",file="~/Dropbox/professional/Research/Active/SciInRegs/Papers/Network/Analysis/SimNets.RData")

save(list="simBCArt",file="~/Dropbox/professional/Research/Active/SciInRegs/Papers/Network/Analysis/simBC.RData")

